<template>
    <div class="app-container">
        <!-- 搜索区域 -->
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd">发布资源</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="fileList" style="width: 100%">
            <el-table-column prop="filename" label="文件名">
            </el-table-column>
            <el-table-column prop="fileType" label="文件类型" width="150">
            </el-table-column>
            <el-table-column prop="fileSize" label="文件大小" width="100">
                <template slot-scope="scope">
                    {{ formatFileSize(scope.row.fileSize) }}
                </template>
            </el-table-column>
            <el-table-column prop="downloadNum" label="下载量" width="100">
            </el-table-column>
            <el-table-column prop="likeNum" label="点赞量" width="100">
            </el-table-column>
            <el-table-column label="操作" width="80">
                <template slot-scope="scope">
                    <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="queryParams.pageNum" :page-sizes="[10, 20, 30, 40]" :page-size="queryParams.pageSize"
            layout="prev, pager, next" :total="total">
        </el-pagination>

        <!-- 编辑弹窗 -->
        <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="资源" prop="fileId">
                    <my-upload :file="JSON.stringify(form.fileData)" :needUpload="true" @callParent="handleUploadSuccess" ref="upload"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { reqFileShareList, reqFileShareAdd, reqFileShareDelete, reqFileShareEdit } from '@/api';
import myUpload from '@/components/upload/file.vue';

export default {
    name: "FileShare",
    components: {
        myUpload
    },
    data() {
        return {
            // 遮罩层
            loading: false,
            // 文件列表
            fileList: [],
            // 总条数
            total: 0,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10
            },
            // 是否显示弹出层
            open: false,
            // 弹出层标题
            title: "",
            // 表单参数
            form: {
                id: undefined,
                fileId: undefined,
                fileData: undefined
            },
            // 表单校验
            rules: {
                
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 格式化文件大小 */
        formatFileSize(size) {
            if (!size) return '0 MB';
            const mb = size / (1024 * 1024);
            return mb.toFixed(2) + ' MB';
        },
        /** 查询文件分享列表 */
        getList() {
            this.loading = true;
            reqFileShareList({
                pageNum: this.queryParams.pageNum,
                pageSize: this.queryParams.pageSize,
                isSelf: true
            }).then(res => {
                this.fileList = res.rows;
                this.total = res.total;
                this.loading = false;
            }).catch(err => {
                this.loading = false;
                this.$message.error(err.msg);
            })
        },
        /** 编辑按钮操作 */
        handleEdit(row) {
            this.title = "编辑资源";
            this.open = true;
            this.form = {
                id: row.id,
                fileId: row.fileId,
                fileData: {
                    filename: row.filename,
                    downloadUrl: row.downloadUrl
                }
            };
            // 设置上传组件的文件数据
            this.$nextTick(() => {
                this.$refs.upload.setFileData(this.form.fileData);
            });
        },
        /** 取消按钮 */
        cancel() {
            this.open = false;
            this.reset();
        },
        /** 表单重置 */
        reset() {
            this.form = {
                id: undefined,
                fileId: undefined,
                fileData: undefined
            };
            // 重置上传组件
            this.$nextTick(() => {
                this.$refs.upload.setFileData(null);
            });
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.title = "发布资源";
            this.open = true;
            this.reset();
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id) {
                        reqFileShareEdit(this.form).then(response => {
                            this.$message.success("提交成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        reqFileShareAdd(this.form).then(response => {
                            this.$message.success("提交成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            this.$confirm('是否确认删除该文件分享?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                return reqFileShareDelete(row.id);
            }).then(() => {
                this.getList();
                this.$message.success("删除成功");
            }).catch(() => { });
        },
        /** 上传成功回调 */
        handleUploadSuccess(data) {
            this.form.fileId = data.replace('http://localhost:8080/oss/download/', '');
        },
        /** 每页条数改变 */
        handleSizeChange(val) {
            this.queryParams.pageSize = val;
            this.getList();
        },
        /** 当前页改变 */
        handleCurrentChange(val) {
            this.queryParams.pageNum = val;
            this.getList();
        }
    }
};
</script>

<style scoped>
.app-container {
    padding: 20px;
}

.mb8 {
    margin-bottom: 8px;
    display: flex;
    justify-content: right;
}

.el-pagination {
    margin-top: 20px;
    text-align: right;
}
</style>